#include "loopsys.h"
#include "console.h"
#include "cli.h"
#include "log.h"
#include "port.h"

#define MSG_ID_TEST1  1
#define MSG_ID_TEST2  2

loop_task_t *task_event1;
loop_task_t *task_event2;
loop_task_t *task_evt_send;
loop_task_t *task_msg_pub;
loop_task_t *task_oneshot;
loop_task_t *task_autorun1;
loop_task_t *task_autorun2;

void func_event1(loop_task_t *self, uint32_t events)
{
    loop_msg_t *msg;
    int32_t i, msg_num;
    
    LOGE("This is func_event1: %x\r\n", events);

    if(events | LOOPSYS_EVENT_MSG)
    {
        msg_num = loopsys_msg_get(self, &msg);
        LOGE("func_event1 get msg num: %d\r\n", msg_num);
        for(i=0; i<msg_num; i++)
        {
            LOGE("func_event1 pub: %d, id: %d, data: %d\r\n", msg->publisher, msg->id, (int32_t)msg->data);
            msg++;
        }
    }

    loopsys_event_clear(self, events);
}

void func_event2(loop_task_t *self, uint32_t events)
{
    loop_msg_t *msg;
    int32_t i, msg_num;
    LOGE("This is func_event2: %x\r\n", events);
    
    if(events | LOOPSYS_EVENT_MSG)
    {
        msg_num = loopsys_msg_get(self, &msg);
        LOGE("func_event2 get msg num: %d\r\n", msg_num);
        for(i=0; i<msg_num; i++)
        {
            LOGE("func_event2 pub: %d, id: %d, data: %d\r\n", msg->publisher, msg->id, (int32_t)msg->data);
            msg++;
        }
    }
    
    loopsys_event_clear(self, events);
}

void func_event_send(loop_task_t *self, uint32_t events)
{
    LOGE("This is func_event_send, send event 0x11\r\n");
    loopsys_event_send(task_event1, 0x11);
    
    loopsys_event_clear_all(self);
}

void func_msg_pub(loop_task_t *self, uint32_t events)
{
    LOGE("This is func_msg_pub, publish msg 88 and 99\r\n");

    loopsys_msg_publish(self, MSG_ID_TEST1, (void *)88);
    loopsys_msg_publish(self, MSG_ID_TEST2, (void *)99);
    
    loopsys_event_clear_all(self);
}

void func_oneshort(loop_task_t *self, uint32_t events)
{
    LOGE("This is func_oneshort\r\n");
    
    loopsys_event_clear_all(self);
}

void func_autorun1(loop_task_t *self, uint32_t events)
{
    LOGE("This is func_autorun1: %x\r\n", events);
    
    loopsys_event_clear_all(self);
}

void func_autorun2(loop_task_t *self, uint32_t events)
{
    LOGE("This is func_autorun2: %x\r\n", events);
    
    loopsys_event_clear_all(self);
}

void test_cmd(int32_t argc, char *args[])
{
    int32_t i;

    for(i=0; i<argc; i++)
        LOGS("%s ", args[i]);
    LOGS("\r\n");
    
    if(argc != 2)
        return;
    
    if(!strcmp(args[1], "on"))
    {
        loopsys_task_timer_start(task_evt_send);
        loopsys_task_timer_start(task_msg_pub);
        loopsys_task_timer_start(task_oneshot);
    }else if(!strcmp(args[1], "off"))
    {
        loopsys_task_timer_stop(task_evt_send);
        loopsys_task_timer_stop(task_msg_pub);
        loopsys_task_timer_stop(task_oneshot);
    }
}

void test_run(void)
{
    extern console_dev_ops_t stm32_console_ops;
    console_init(&stm32_console_ops);

    console_task_init();    
    cli_task_init();
    
    cli_register_cmd("test_cmd", test_cmd);
    
    LOGS_EN(1);
    LOGE_EN(1);

    task_event1 = loopsys_task_add(0, func_event1);
    if(!task_event1)
        LOGE("task_event1 add failed\r\n");
    
    task_event2 = loopsys_task_add(1, func_event2);
    if(!task_event2)
        LOGE("task_event2 add failed\r\n");
    
    task_evt_send = loopsys_task_add(2, func_event_send);
    if(!task_evt_send)
        LOGE("task_evt_send add failed\r\n");
    
    task_msg_pub = loopsys_task_add(3, func_msg_pub);
    if(!task_msg_pub)
        LOGE("task_msg_pub add failed\r\n");
    
    task_oneshot = loopsys_task_add(4, func_oneshort);
    if(!task_oneshot)
        LOGE("task_oneshot add failed\r\n");
    
    task_autorun1 = loopsys_task_add(5, func_autorun1);
    if(!task_autorun1)
        LOGE("task_autorun1 add failed\r\n");
    
    task_autorun2 = loopsys_task_add(6, func_autorun2);
    if(!task_autorun2)
        LOGE("task_autorun2 add failed\r\n");
    
    loopsys_msg_subscribe(task_event1, MSG_ID_TEST1);
    loopsys_msg_subscribe(task_event1, MSG_ID_TEST2);
    loopsys_msg_subscribe(task_event2, MSG_ID_TEST1);
    loopsys_msg_subscribe(task_event2, MSG_ID_TEST2);
    
    loopsys_task_timer_enable(task_evt_send, LOOPSYS_TIMER_FOREVER, 1000);
    loopsys_task_timer_enable(task_msg_pub, LOOPSYS_TIMER_FOREVER, 2000);
    loopsys_task_timer_enable(task_oneshot, 3, 5000);
    //loopsys_task_autorun_enable(task_autorun1);
    //loopsys_task_autorun_enable(task_autorun2);
    
    loopsys_task_timer_start(task_evt_send);
    loopsys_task_timer_start(task_msg_pub);
    loopsys_task_timer_start(task_oneshot);

    while(1)
    {
        loopsys_run();
    }
}
